package com.wobito.controller.admin;


import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wobito.common.constants.BaseResponse;
import com.wobito.common.constants.BusinessException;
import com.wobito.common.constants.ErrorCode;
import com.wobito.common.constants.TableDataInfo;
import com.wobito.common.utils.DateUtils;
import com.wobito.common.utils.ResultUtils;
import com.wobito.pojo.bean.Area;
import com.wobito.pojo.dto.area.AreaDTO;
import com.wobito.pojo.enums.IsDeletedEnum;
import com.wobito.pojo.vo.area.AddAreaVO;
import com.wobito.pojo.vo.area.AreaVO;
import com.wobito.pojo.vo.area.UpdateAreaVO;
import com.wobito.service.IAreaService;
import com.wobito.system.aspect.OperLog;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 区域表 前端控制器
 * </p>
 *
 * @author lambyang
 * @since 2024-07-23
 */
@RestController
@RequestMapping("/manage/areaManage")
@Api(tags = "区域管理")
public class AreaManageController{

    @Autowired
    private IAreaService areaService;

    @GetMapping()
    @ApiOperation(value = "查询区域")
    @OperLog(thing = "查询区域")
    @PreAuthorize("hasAnyRole('SUPER_ADMIN','NOMAL_ADMIN')")
    public BaseResponse getAreaList(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                    @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                    @RequestParam(value = "queryName", required = false) String queryName,
                                    @RequestParam(value = "isDeleted", required = false) Integer isDeleted) {
        TableDataInfo dataInfo = new TableDataInfo();
        PageHelper.startPage(pageNum, pageSize);
        List<AreaDTO> areaDTOS = areaService.selectAreaList(new AreaVO(queryName, isDeleted));
        dataInfo.setRows(areaDTOS);
        dataInfo.setTotal(new PageInfo(areaDTOS).getTotal());
        return ResultUtils.success(dataInfo);
    }

    @GetMapping("/getAreaList")
    @ApiOperation(value = "获取区域(ID-NAME)", notes = "下拉框选择等用，只会返回正常状态的")
    @OperLog(thing = "获取区域下拉框列表")
    @PreAuthorize("hasAnyRole('SUPER_ADMIN','NOMAL_ADMIN')")
    public BaseResponse getAreaBriefList() {
        return ResultUtils.success(areaService.selectBrief());
    }

    @PutMapping()
    @ApiOperation(value = "添加区域")
    @OperLog(thing = "添加区域")
    @PreAuthorize("hasAnyRole('SUPER_ADMIN','NOMAL_ADMIN')")
    public BaseResponse addArea(@Valid @RequestBody AddAreaVO areaVO) {
        Area area = areaService.getOne(new LambdaQueryWrapper<Area>().eq(Area::getName, areaVO.getName()));
        if (ObjectUtil.isNotEmpty(area)) {
            throw new BusinessException(ErrorCode.REPEATPARAMS_ERROR, "此区域已存在");
        }
        return ResultUtils.success(areaService.insertArea(areaVO));
    }

    @PutMapping("/startArea/{id}")
    @ApiOperation(value = "启用区域")
    @OperLog(thing = "启用区域")
    @PreAuthorize("hasAnyRole('SUPER_ADMIN','NOMAL_ADMIN')")
    public BaseResponse startArea(@PathVariable("id")Long id) {
        return ResultUtils.success(areaService.updateById(new Area(id, IsDeletedEnum.NORMAL.getCode())));
    }

    @PostMapping()
    @ApiOperation(value = "修改区域")
    @OperLog(thing = "修改区域")
    @PreAuthorize("hasAnyRole('SUPER_ADMIN','NOMAL_ADMIN')")
    public BaseResponse updateArea(@Valid @RequestBody UpdateAreaVO updateAreaVO) {
        Area area = areaService.getOne(new LambdaQueryWrapper<Area>().eq(Area::getName, updateAreaVO.getName()));
        if (ObjectUtil.isNotEmpty(area) && !area.getId().equals(updateAreaVO.getId())) {
            throw new BusinessException(ErrorCode.REPEATPARAMS_ERROR, "此区域已存在");
        }
        return ResultUtils.success(areaService.updateArea(updateAreaVO));
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "停用区域")
    @OperLog(thing = "停用区域")
    @PreAuthorize("hasAnyRole('SUPER_ADMIN','NOMAL_ADMIN')")
    public BaseResponse delArea(@PathVariable("id") Long id) {
        return ResultUtils.success(areaService.updateById(new Area(id, IsDeletedEnum.DELETED.getCode(), DateUtils.getNowDate())));
    }

}
